package org.rcy.ruicingmarket.controller;


import org.rcy.ruicingmarket.activity.ActivityEditVo;
import org.rcy.ruicingmarket.activity.ActivityQueryCondition;
import org.rcy.ruicingmarket.activity.ActivityService;
import org.rcy.ruicingmarket.activity.ActivityShowVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author RuicyWu
 * @Description
 * @date 2022/2/8 15:26
 */

@RestController
@RequestMapping("/activity")
public class ActivityController {
    protected final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ActivityService activityService;

    @PostMapping("/querypage")
    public ResultMessage queryPage(@RequestBody ActivityQueryCondition condition){
        try {
            return ResultMessage.success(activityService.queryPage(condition));
        }catch (Exception e){
            logger.error(e.getMessage());
            return ResultMessage.error();
        }
    }

    @GetMapping("/{id}")
    public ResultMessage<ActivityShowVo> getById(@PathVariable("id") Long id){
        try {
            return ResultMessage.success(activityService.findById(id));
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            return ResultMessage.error();
        }
    }

    @PutMapping("/edit")
    public ResultMessage editActivity(@RequestBody ActivityEditVo vo){
        try {
            activityService.edit(vo);
            return ResultMessage.success();
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            return ResultMessage.error();
        }
    }

    @PostMapping("/add")
    public ResultMessage addActivity(@RequestBody ActivityEditVo vo){
        try {
            activityService.add(vo);
            return ResultMessage.success();
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            return ResultMessage.error();
        }
    }

    @DeleteMapping("/remove/{id}")
    public ResultMessage removeActivity(@PathVariable("id")Long id){
        try {
            activityService.remove(id);
            return ResultMessage.success();
        }catch (Exception e){
            logger.error(e.getMessage(),e);
            return ResultMessage.error();
        }
    }

    @GetMapping("/getall")
    public ResultMessage getAll(){
        try {
           return ResultMessage.success(activityService.findAll());
        }catch (Exception e){
          logger.info(e.getMessage(),e);
          return ResultMessage.error();
        }
    }
}
